package com.eastfair.certificate.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.tenant.TenantAuth;
import com.eastfair.certificate.dto.*;
import com.eastfair.certificate.entity.CarRecord;
import com.eastfair.certificate.entity.CertificateUser;
import com.eastfair.certificate.service.CarRecordService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.certificate.service.CertificateOrderService;
import com.eastfair.certificate.service.CertificateUserService;
import com.eastfair.certificate.vo.CarRecordVO;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 车辆出入证信息
 * </p>
 *
 * @author ligang
 * @date 2023-01-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/carRecord")
@Api(value = "CarRecord", tags = "车辆出入证信息")
@PreAuth(replace = "certificate:carRecord:")
public class CarRecordController extends SuperController<CarRecordService, Long, CarRecord, CarRecordPageQuery, CarRecordSaveDTO, CarRecordUpdateDTO> {

    @Autowired
    private EchoService echoService;


    @Autowired
    private CertificateUserService certificateUserService;


    @Override
    public void handlerResult(IPage<CarRecord> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<CarRecord> carRecordList = list.stream().map((map) -> {
            CarRecord carRecord = CarRecord.builder().build();
            //TODO 请在这里完成转换
            return carRecord;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(carRecordList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<CarRecordPageQuery> params) {
        CarRecordPageQuery pageQuery = params.getModel();

    }


    /**
     * 查询车辆出入信息
     *
     * @param carRecordDTO
     * @return
     */
    @ApiOperation(value = "查询车辆出入信息", notes = "查询车辆出入信息")
    @PostMapping("/queryCar")
    @BaseControllerInfo()
    @SysLog("查询车辆出入信息")
    public R queryCarDetail(@RequestBody CarRecordDTO carRecordDTO) {
        CertificateUserDTO certificateUserDTO = new CertificateUserDTO();
        certificateUserDTO.setOrderId(carRecordDTO.getOrderId());
        List<CertificateUser> certificateUserList = certificateUserService.list(certificateUserDTO, null);
        if (CollectionUtil.isEmpty(certificateUserList)) {
            return R.success();
        }
        List<String> carNumberList = certificateUserList.stream().map(CertificateUser::getCarNumber).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(carNumberList)) {
            return R.success();
        }
        carRecordDTO = new CarRecordDTO();
        carRecordDTO.setCarCodes(carNumberList);
        List<CarRecordVO> carRecordVOList = baseService.queryCar(carRecordDTO);
        return R.success(baseService.assmbileCarRecord(carRecordVOList, certificateUserList));
    }


}
